package com.exam.controller;

import com.exam.entity.ClassScore;
import com.exam.service.ClassScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/class-scores")
public class ClassScoreController {

    @Autowired
    private ClassScoreService classScoreService;

    // 分页查询班级成绩
    @GetMapping("/{classId}")
    public Page<ClassScore> getScoresByClassId(
            @PathVariable Integer classId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return classScoreService.getScoresByClassId(classId, pageable);
    }
//@PathVariable Integer classId：从URL路径中获取classId参数。
//@RequestParam(defaultValue = "0") int page和@RequestParam(defaultValue = "10") int size：从请求参数中获取分页信息，默认页码为0，每页大小为10。
//Pageable pageable = PageRequest.of(page, size);：创建分页请求对象。
//return classScoreService.getScoresByClassId(classId, pageable);：调用服务层方法获取分页后的班级成绩。
    // 按课程分页查询班级成绩
    @GetMapping("/{classId}/course")
    public Page<ClassScore> getScoresByClassIdAndCourse(
            @PathVariable Integer classId,
            @RequestParam String courseName,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return classScoreService.getScoresByClassIdAndCourse(classId, courseName, pageable);
    }
//@PathVariable Integer classId：从URL路径中获取classId参数。
//@RequestParam String courseName：从请求参数中获取课程名称。 @RequestParam(defaultValue = "0") int page和@RequestParam(defaultValue = "10") int size：从请求参数中获取分页信息，默认页码为0，每页大小为10。
//Pageable pageable = PageRequest.of(page, size);：创建分页请求对象。
    // 保存或更新班级成绩
    @PostMapping
    public ClassScore saveOrUpdateClassScore(@RequestBody ClassScore classScore) {
        return classScoreService.saveOrUpdateClassScore(classScore);
    }

    // 删除班级成绩
    @DeleteMapping("/{classScoreId}")
    public void deleteClassScoreById(@PathVariable Integer classScoreId) {
        classScoreService.deleteClassScoreById(classScoreId);
    }
}